
/**
 * 
 * @author 魏康威
 *
 */
public class BinarySearch {

	static int  findLastLE(int[] array, int n, int target) // satisfy condition: array[?] <= target and the last one
	{
		int start = 0, end = n - 1;
		while (start <= end) {
			int mid = (start + end) / 2;
			if (array[mid] <= target)
				start = mid + 1;
			else if (array[mid] > target)
				end = mid - 1;
		}
		
		if (start == 0)
			return -1;
	 
		return start - 1;
	}
	
	public static void main(String[] args) {
		int[] nums = {-4, 1, 2, 2, 5, 5, 5, 8, 10};
		int sub = findLastLE(nums,nums.length,6);
		System.out.println("小于等于6的最后一个数的下标为:" + sub  + ",其对应的数组值为：nums["+ sub +"]" + "：" + nums[sub]);
	}
	
}
